

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>AG_fft_tools.smooth_tools &mdash; agpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../_static/extra.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="agpy 0.1.2 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" />
     
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setDomainName', 'pyspeckit.bitbucket.org']);
      _gaq.push(['_setAllowHash', false]);
      _gaq.push(['_trackPageview']);


    </script>
    <link rel="stylesheet" type="text/css" href="../../_static/extra.css" />
  </head>
  <body>
    <div class="header-wrapper">
      <div class="header">
        <h1><a href="../../index.html">agpy 0.1.2 documentation</a></h1>
        <div class="rel">
          <a href="http://agpy.googlecode.com">agpy Home </a>  |
          <a href=../../index.html>Docs Home </a>  |
          <a href="http://code.google.com/p/agpy/w/list">Wiki</a>  |
          <a href=../../search.html>Search </a>
        </div>
       </div>
    </div>

    <div class="content-wrapper">
      <div class="content">
        <div class="document">
            
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for AG_fft_tools.smooth_tools</h1><div class="highlight"><pre>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">types</span>
<span class="kn">from</span> <span class="nn">AG_image_tools</span> <span class="kn">import</span> <span class="n">downsample</span> <span class="k">as</span> <span class="n">downsample_2d</span>
<span class="kn">from</span> <span class="nn">convolve_nd</span> <span class="kn">import</span> <span class="n">convolvend</span> <span class="k">as</span> <span class="n">convolve</span>

<div class="viewcode-block" id="smooth"><a class="viewcode-back" href="../../fft_tools.html#AG_fft_tools.smooth_tools.smooth">[docs]</a><span class="k">def</span> <span class="nf">smooth</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">kernelwidth</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">kerneltype</span><span class="o">=</span><span class="s">&#39;gaussian&#39;</span><span class="p">,</span> <span class="n">trapslope</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
        <span class="n">silent</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">psf_pad</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">interp_nan</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">nwidths</span><span class="o">=</span><span class="s">&#39;max&#39;</span><span class="p">,</span>
        <span class="n">min_nwidths</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">return_kernel</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">normalize_kernel</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">,</span>
        <span class="n">downsample</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">downsample_factor</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns a smoothed image using a gaussian, boxcar, or tophat kernel</span>

<span class="sd">    Options: </span>
<span class="sd">    kernelwidth:</span>
<span class="sd">        width of kernel in pixels  (see definitions below)</span>
<span class="sd">    kerneltype:</span>
<span class="sd">        gaussian, boxcar, or tophat.  </span>
<span class="sd">        For a gaussian, uses a gaussian with sigma = kernelwidth (in pixels)</span>
<span class="sd">            out to [nwidths]-sigma</span>
<span class="sd">        A boxcar is a kernelwidth x kernelwidth square </span>
<span class="sd">        A tophat is a flat circle with radius = kernelwidth</span>
<span class="sd">    Default options:</span>
<span class="sd">        psf_pad: [True]</span>
<span class="sd">            will pad the input image to be the image size + PSF.</span>
<span class="sd">            Slows things down but removes edge-wrapping effects (see convolve)</span>
<span class="sd">            This option should be set to false if the edges of your image are</span>
<span class="sd">            symmetric.</span>
<span class="sd">        interp_nan: [False]</span>
<span class="sd">            Will replace NaN points in an image with the</span>
<span class="sd">            smoothed average of its neighbors (you can still simply ignore NaN </span>
<span class="sd">            values by setting ignore_nan=True but leaving interp_nan=False)</span>
<span class="sd">        silent: [True]</span>
<span class="sd">            turn it off to get verbose statements about kernel types</span>
<span class="sd">        return_kernel: [False]</span>
<span class="sd">            If set to true, will return the kernel as the</span>
<span class="sd">            second return value</span>
<span class="sd">        nwidths: [&#39;max&#39;]</span>
<span class="sd">            number of kernel widths wide to make the kernel.  Set to &#39;max&#39; to</span>
<span class="sd">            match the image shape, otherwise use any integer </span>
<span class="sd">        min_nwidths: [6]</span>
<span class="sd">            minimum number of gaussian widths to make the kernel</span>
<span class="sd">            (the kernel will be larger than the image if the image size is &lt;</span>
<span class="sd">            min_widths*kernelsize)</span>
<span class="sd">        normalize_kernel:</span>
<span class="sd">            Should the kernel preserve the map sum (i.e. kernel.sum() = 1)</span>
<span class="sd">            or the kernel peak (i.e. kernel.max() = 1) ?  Must be a *function* that can</span>
<span class="sd">            operate on a numpy array</span>
<span class="sd">        downsample:</span>
<span class="sd">            downsample after smoothing?</span>
<span class="sd">        downsample_factor:</span>
<span class="sd">            if None, default to kernelwidth</span>

<span class="sd">    Note that the kernel is forced to be even sized on each axis to assure no</span>
<span class="sd">    offset when smoothing.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">kernelwidth</span><span class="o">*</span><span class="n">min_nwidths</span> <span class="o">&gt;</span> <span class="n">image</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">or</span> <span class="n">kernelwidth</span><span class="o">*</span><span class="n">min_nwidths</span> <span class="o">&gt;</span> <span class="n">image</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
        <span class="n">nwidths</span> <span class="o">=</span> <span class="n">min_nwidths</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">nwidths</span><span class="o">!=</span><span class="s">&#39;max&#39;</span><span class="p">):</span><span class="c"># and kernelwidth*nwidths &lt; image.shape[0] and kernelwidth*nwidths &lt; image.shape[1]):</span>
        <span class="n">dimsize</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">kernelwidth</span><span class="o">*</span><span class="n">nwidths</span><span class="p">)</span>
        <span class="n">dimsize</span> <span class="o">+=</span> <span class="n">dimsize</span> <span class="o">%</span> <span class="mi">2</span>
        <span class="n">yy</span><span class="p">,</span><span class="n">xx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">indices</span><span class="p">([</span><span class="n">dimsize</span><span class="p">,</span><span class="n">dimsize</span><span class="p">])</span>
        <span class="n">szY</span><span class="p">,</span><span class="n">szX</span> <span class="o">=</span> <span class="n">dimsize</span><span class="p">,</span><span class="n">dimsize</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">szY</span><span class="p">,</span><span class="n">szX</span> <span class="o">=</span> <span class="n">image</span><span class="o">.</span><span class="n">shape</span>
        <span class="n">szY</span> <span class="o">+=</span> <span class="n">szY</span> <span class="o">%</span> <span class="mi">2</span>
        <span class="n">szX</span> <span class="o">+=</span> <span class="n">szX</span> <span class="o">%</span> <span class="mi">2</span>
        <span class="n">yy</span><span class="p">,</span><span class="n">xx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">indices</span><span class="p">([</span><span class="n">szY</span><span class="p">,</span><span class="n">szX</span><span class="p">])</span>
    <span class="n">shape</span> <span class="o">=</span> <span class="p">(</span><span class="n">szY</span><span class="p">,</span><span class="n">szX</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">silent</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;Kernel size set to &quot;</span><span class="p">,</span><span class="n">shape</span>
    <span class="n">rr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">xx</span><span class="o">-</span><span class="n">szX</span><span class="o">/</span><span class="mf">2.</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="o">+</span><span class="p">(</span><span class="n">yy</span><span class="o">-</span><span class="n">szY</span><span class="o">/</span><span class="mf">2.</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">kerneltype</span> <span class="o">==</span> <span class="s">&#39;gaussian&#39;</span><span class="p">:</span>
        <span class="n">kernel</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">rr</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mf">2.</span><span class="o">*</span><span class="n">kernelwidth</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
        <span class="n">kernel</span> <span class="o">/=</span> <span class="n">normalize_kernel</span><span class="p">(</span><span class="n">kernel</span><span class="p">)</span> <span class="c">#/ (kernelwidth**2 * (2*np.pi))</span>
<span class="c">#        if kernelwidth != np.round(kernelwidth):</span>
<span class="c">#            print &quot;Rounding kernel width to %i pixels&quot; % np.round(kernelwidth)</span>
<span class="c">#            kernelwidth = np.round(kernelwidth)</span>

    <span class="k">elif</span> <span class="n">kerneltype</span> <span class="o">==</span> <span class="s">&#39;boxcar&#39;</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">silent</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;Using boxcar kernel size </span><span class="si">%i</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">kernelwidth</span><span class="p">)</span>
        <span class="n">kernel</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">kernelwidth</span><span class="p">),</span><span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">kernelwidth</span><span class="p">)],</span><span class="n">dtype</span><span class="o">=</span><span class="s">&#39;float64&#39;</span><span class="p">)</span> <span class="o">/</span> <span class="n">kernelwidth</span><span class="o">**</span><span class="mi">2</span>
        <span class="n">kernel</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="s">&#39;float64&#39;</span><span class="p">)</span>
        <span class="n">kernel</span><span class="p">[((</span><span class="n">xx</span><span class="o">-</span><span class="n">szX</span><span class="o">/</span><span class="mf">2.</span><span class="p">)</span><span class="o">&lt;=</span><span class="n">kernelwidth</span><span class="p">)</span><span class="o">*</span><span class="p">((</span><span class="n">yy</span><span class="o">&lt;=</span><span class="n">szY</span><span class="o">/</span><span class="mf">2.</span><span class="p">)</span><span class="o">&lt;=</span><span class="n">kernelwidth</span><span class="p">)]</span> <span class="o">=</span> <span class="mf">1.0</span>
        <span class="n">kernel</span> <span class="o">/=</span> <span class="n">normalize_kernel</span><span class="p">(</span><span class="n">kernel</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">kerneltype</span> <span class="o">==</span> <span class="s">&#39;tophat&#39;</span><span class="p">:</span>
        <span class="n">kernel</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="s">&#39;float64&#39;</span><span class="p">)</span>
        <span class="n">kernel</span><span class="p">[</span><span class="n">rr</span><span class="o">&lt;</span><span class="n">kernelwidth</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
        <span class="c"># normalize</span>
        <span class="n">kernel</span> <span class="o">/=</span> <span class="n">normalize_kernel</span><span class="p">(</span><span class="n">kernel</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">kerneltype</span> <span class="o">==</span> <span class="s">&#39;brickwall&#39;</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">silent</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;Smoothing with a </span><span class="si">%i</span><span class="s"> pixel airy function&quot;</span> <span class="o">%</span> <span class="n">kernelwidth</span>
        <span class="c"># airy function is first bessel(x) / x  [like the sinc]</span>
        <span class="k">print</span> <span class="s">&quot;WARNING: I think the Airy should be (2*Besel(x)/x)^2?&quot;</span> <span class="c"># http://en.wikipedia.org/wiki/Airy_disk</span>
        <span class="n">kernel</span> <span class="o">=</span> <span class="n">j1</span><span class="p">(</span><span class="n">rr</span><span class="o">/</span><span class="n">kernelwidth</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">rr</span><span class="o">/</span><span class="n">kernelwidth</span><span class="p">)</span> 
        <span class="c"># fix NAN @ center</span>
        <span class="n">kernel</span><span class="p">[</span><span class="n">rr</span><span class="o">==</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.5</span>
        <span class="c"># normalize - technically, this should work, but practically, flux is GAINED in some images.  </span>
        <span class="n">kernel</span> <span class="o">/=</span> <span class="n">normalize_kernel</span><span class="p">(</span><span class="n">kernel</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">kerneltype</span> <span class="o">==</span> <span class="s">&#39;trapezoid&#39;</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">trapslope</span><span class="p">:</span>
            <span class="n">zz</span> <span class="o">=</span> <span class="n">rr</span><span class="o">.</span><span class="n">max</span><span class="p">()</span><span class="o">-</span><span class="p">(</span><span class="n">rr</span><span class="o">*</span><span class="n">trapslope</span><span class="p">)</span>
            <span class="n">zz</span><span class="p">[</span><span class="n">zz</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">zz</span><span class="p">[</span><span class="n">rr</span><span class="o">&lt;</span><span class="n">kernelwidth</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
            <span class="n">kernel</span> <span class="o">=</span> <span class="n">zz</span><span class="o">/</span><span class="n">zz</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">silent</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;trapezoid function requires a slope&quot;</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">silent</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;Kernel of type </span><span class="si">%s</span><span class="s"> normalized with </span><span class="si">%s</span><span class="s"> has peak </span><span class="si">%g</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">kerneltype</span><span class="p">,</span> <span class="n">normalize_kernel</span><span class="p">,</span> <span class="n">kernel</span><span class="o">.</span><span class="n">max</span><span class="p">())</span>

    <span class="n">bad</span> <span class="o">=</span> <span class="p">(</span><span class="n">image</span> <span class="o">!=</span> <span class="n">image</span><span class="p">)</span>
    <span class="n">temp</span> <span class="o">=</span> <span class="n">image</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> <span class="c"># to preserve NaN values</span>
    <span class="c"># convolve does this already temp[bad] = 0</span>

    <span class="c"># kwargs parsing to avoid duplicate keyword passing</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">&#39;ignore_zeros&#39;</span><span class="p">):</span> <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;ignore_zeros&#39;</span><span class="p">]</span><span class="o">=</span><span class="bp">True</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">&#39;ignore_nan&#39;</span><span class="p">):</span> <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;ignore_nan&#39;</span><span class="p">]</span><span class="o">=</span><span class="n">interp_nan</span>

    <span class="c"># No need to normalize - normalization is dealt with in this code</span>
    <span class="n">temp</span> <span class="o">=</span> <span class="n">convolve</span><span class="p">(</span><span class="n">temp</span><span class="p">,</span><span class="n">kernel</span><span class="p">,</span><span class="n">psf_pad</span><span class="o">=</span><span class="n">psf_pad</span><span class="p">,</span> <span class="n">normalize_kernel</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">interp_nan</span> <span class="ow">is</span> <span class="bp">False</span><span class="p">:</span> <span class="n">temp</span><span class="p">[</span><span class="n">bad</span><span class="p">]</span> <span class="o">=</span> <span class="n">image</span><span class="p">[</span><span class="n">bad</span><span class="p">]</span>

    <span class="k">if</span> <span class="n">temp</span><span class="o">.</span><span class="n">shape</span> <span class="o">!=</span> <span class="n">image</span><span class="o">.</span><span class="n">shape</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Output image changed size; this is completely impossible.&quot;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">downsample</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">downsample_factor</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">downsample_factor</span> <span class="o">=</span> <span class="n">kernelwidth</span>
        <span class="k">if</span> <span class="n">return_kernel</span><span class="p">:</span> <span class="k">return</span> <span class="n">downsample_2d</span><span class="p">(</span><span class="n">temp</span><span class="p">,</span><span class="n">downsample_factor</span><span class="p">),</span><span class="n">downsample_2d</span><span class="p">(</span><span class="n">kernel</span><span class="p">,</span><span class="n">downsample_factor</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">downsample_2d</span><span class="p">(</span><span class="n">temp</span><span class="p">,</span><span class="n">downsample_factor</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">return_kernel</span><span class="p">:</span> <span class="k">return</span> <span class="n">temp</span><span class="p">,</span><span class="n">kernel</span>
        <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">temp</span>
</div>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">def make_kernel(kernelshape, kernelwidth=3, kerneltype=&#39;gaussian&#39;, silent=True):</span>
<span class="sd">    yy,xx = np.indices(kernelshape)</span>
<span class="sd">    szY,szX = kernelshape</span>
<span class="sd">    if not silent: print &quot;Kernel size set to &quot;,kernelshape</span>
<span class="sd">    rr = np.sqrt((xx-szX/2.)**2+(yy-szY/2.)**2)</span>

<span class="sd">    if kerneltype == &#39;gaussian&#39;:</span>
<span class="sd">        kernel = np.exp(-(rr**2)/(2.*kernelwidth**2))</span>
<span class="sd">        kernel /= normalize_kernel(kernel) #/ (kernelwidth**2 * (2*np.pi))</span>
<span class="sd">#        if kernelwidth != np.round(kernelwidth):</span>
<span class="sd">#            print &quot;Rounding kernel width to %i pixels&quot; % np.round(kernelwidth)</span>
<span class="sd">#            kernelwidth = np.round(kernelwidth)</span>

<span class="sd">    elif kerneltype == &#39;boxcar&#39;:</span>
<span class="sd">        if not silent: print &quot;Using boxcar kernel size %i&quot; % np.ceil(kernelwidth)</span>
<span class="sd">        kernel = np.ones([np.ceil(kernelwidth),np.ceil(kernelwidth)],dtype=&#39;float64&#39;) / kernelwidth**2</span>
<span class="sd">        kernel = np.zeros(shape,dtype=&#39;float64&#39;)</span>
<span class="sd">        kernel[((xx-szX/2.)&lt;=kernelwidth)*((yy&lt;=szY/2.)&lt;=kernelwidth)] = 1.0</span>
<span class="sd">        kernel /= normalize_kernel(kernel)</span>
<span class="sd">    elif kerneltype == &#39;tophat&#39;:</span>
<span class="sd">        kernel = np.zeros(shape,dtype=&#39;float64&#39;)</span>
<span class="sd">        kernel[rr&lt;kernelwidth] = 1.0</span>
<span class="sd">        # normalize</span>
<span class="sd">        kernel /= normalize_kernel(kernel)</span>
<span class="sd">    elif kerneltype == &#39;brickwall&#39;:</span>
<span class="sd">        if not silent: print &quot;Smoothing with a %i pixel airy function&quot; % kernelwidth</span>
<span class="sd">        # airy function is first bessel(x) / x  [like the sinc]</span>
<span class="sd">        print &quot;WARNING: I think the Airy should be (2*Besel(x)/x)^2?&quot; # http://en.wikipedia.org/wiki/Airy_disk</span>
<span class="sd">        kernel = j1(rr/kernelwidth) / (rr/kernelwidth) </span>
<span class="sd">        # fix NAN @ center</span>
<span class="sd">        kernel[rr==0] = 0.5</span>
<span class="sd">        # normalize - technically, this should work, but practically, flux is GAINED in some images.  </span>
<span class="sd">        kernel /= normalize_kernel(kernel)</span>
<span class="sd">    elif kerneltype == &#39;trapezoid&#39;:</span>
<span class="sd">        if trapslope:</span>
<span class="sd">            zz = rr.max()-(rr*trapslope)</span>
<span class="sd">            zz[zz&lt;0] = 0</span>
<span class="sd">            zz[rr&lt;kernelwidth] = 1.0</span>
<span class="sd">            kernel = zz/zz.sum()</span>
<span class="sd">        else:</span>
<span class="sd">            if not silent: print &quot;trapezoid function requires a slope&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
</pre></div>

          </div>
        </div>
      </div>
        </div>
        <div class="sidebar">
          <h3>Table Of Contents</h3>
          <ul>
<li class="toctree-l1"><a class="reference internal" href="../../agpy.html">Adam Ginsburg&#8217;s Python Code (agpy)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../image_tools.html">Image Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fft_tools.html">AG_fft_tools Package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../plfit.html">plfit Package</a></li>
</ul>

          <h3 style="margin-top: 1.5em;">Search</h3>
          <form class="search" action="../../search.html" method="get">
            <input type="text" name="q" />
            <input type="submit" value="Go" />
            <input type="hidden" name="check_keywords" value="yes" />
            <input type="hidden" name="area" value="default" />
          </form>
          <p class="searchtip" style="font-size: 90%">
            Enter search terms or a module, class or function name.
          </p>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

    <div class="footer">
      &copy; Copyright 2011, Adam Ginsburg.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2pre.
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-6253248-2']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();

    </script>
        
    </div>
  </body>
</html>